var fid = RequestURLParam.getParam("fid");
var type = RequestURLParam.getParam("type");
var cgi_moduleId = RequestURLParam.getParam("moduleId") || '';
var cgi_moduleName = decodeURIComponent(RequestURLParam.getParam("moduleName") || '');
var objForm = null;
var gridList = null;
var urlTxt = "/wf/form/insert";
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

$(document).ready(function () {
    objForm = $('#form1');
    gridList = $('gridList');

    if (type == "edit") {
        urlTxt = "/wf/form/update";
        init_data();
    }

    if (cgi_moduleId != '')
        $('#moduleId').val(cgi_moduleId);
    if (cgi_moduleName != '')
        $('#moduleName').val(cgi_moduleName);
});

function init_data(){
    ActHttpRequest("/wf/form/findById",{"id":fid},function(data){
        if(data.OK!="OK"){
            layer.msg(data.message, {
                icon: 1,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
            }, function () {
            })
            return;
        }
        $("#form1 .formEdit").each(function(){
            $('#' + this.id).val(data.wfForm[this.id]);
        });

        if (cgi_moduleName != '')
            $('#moduleName').val(cgi_moduleName);
    },null,'加载中。。。');
}

function saveData(){
    var formData=$("#form1").serializeJSON();
    ActHttpRequest(urlTxt,formData,function(data){
        if(data.OK=="OK"){
            parent.layer.msg('成功！', {icon: 1, shade: [0.3, '#000'], shadeClose: true});
            parent.layer.close(index);
        }else{
            layer.open({
                type: 1
                ,offset: type //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                ,id: 'layerDemo'+type //防止重复弹出
                ,content: '<div style="padding: 20px 100px;">'+ text +'</div>'
                ,btn: '关闭全部'
                ,btnAlign: 'c' //按钮居中
                ,shade: 0 //不显示遮罩
                ,yes: function(){
                    layer.closeAll();
                }
            });
        }
    });
}